package com.guojian.jdbc.mapper;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.JdbcUtils;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Maps;
import com.guojian.common.utils.StringUtilss;

public class StringMapRowMapper implements RowMapper<Map<String, String>> {

	@Override
	public Map<String, String> mapRow(ResultSet rs, int rowNum) throws SQLException {
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
		Map<String, String> mapOfColValues = createColumnMap();
		for (int i = 1; i <= columnCount; i++) {
			String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));
			String obj = getColumnValue(rs, i);
			mapOfColValues.put(key, obj);
			mapOfColValues.put(StringUtilss.toLowerCaseOfHump(key), obj);
		}
		return mapOfColValues;
	}

	protected Map<String, String> createColumnMap() {
		// return new
		// LinkedCaseInsensitiveMap<String>(columnCount);//不区分大小写,小写会把大写的变量名覆盖
		return Maps.newLinkedHashMap();
	}

	protected String getColumnKey(String columnName) {
		return columnName;
	}

	protected String getColumnValue(ResultSet rs, int index) throws SQLException {
		return ObjectUtils.toString(JdbcUtils.getResultSetValue(rs, index)).trim();
	}

	public static void main(String[] args) {
		System.out.println(StringUtils.toRootLowerCase("Abc_EFD_e"));// abc_efd_e
		System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "TESTDATA"));// testdata
		System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "TEST_DATA"));// testData
		System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));// testData
		System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_Data"));// testData

	}

}
